import 'package:flutter/material.dart';
import 'package:flutter_share/pages/home/components/notice_detail.dart';
import 'package:flutter_share/pages/login/index.dart';
import 'package:flutter_share/pages/profile/index.dart';
import 'package:flutter_share/pages/tabs_page.dart';
import 'package:flutter_share/utils/token_manager.dart';

void main() {
  runApp(MaterialApp(
    initialRoute: '/', // 设置初始路由
    routes: {
      '/': (context) => const TabsPage(),
      "/notice_detail": (context) => const NoticeDetail(),
      "/login": (context) => const LoginPage(),
      //"/profile": (context) => const ProfilePage(),
    },
    onGenerateRoute: (setting) {
      // 如果跳转了一个没有定义的路由 就会进入onGenerateRoute方法
      // 根据判断 动态生成一个组件
      // profile 的页面有token才可以进入 没有token去登录页
      var token = tokenManager.getToken();
      if (token.isEmpty) {
        return MaterialPageRoute(builder: (context) => const LoginPage());
      } else {
        if (setting.name == "/profile") {
          return MaterialPageRoute(
              builder: (context) => ProfilePage(
                userInfo: setting.arguments as Map<String, dynamic>,
              ),
            );
        }
      }
      return null;
      // 这里要立刻返回
    },
  ));
}
